package com.sw.quartz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServlet;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.sw.watcher.AIWatcher;
import com.sw.watcher.ArbWatcher;
import com.sw.watcher.CourtWatcher;
import com.sw.watcher.EpanWatcher;
import com.sw.watcher.FayuanWatcher;
import com.sw.watcher.ODRWatcher;

public class WatchScheduler extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void init() {
		run();
	}

	private static int interval = 1;

	public static void run() {
		JobDetail courtJob = JobBuilder.newJob(CourtWatcher.class)
				.withIdentity("courtJob", "group_1").build();
		JobDetail arbJob = JobBuilder.newJob(ArbWatcher.class)
				.withIdentity("arbJob", "group_1").build();
		JobDetail epanJob = JobBuilder.newJob(EpanWatcher.class)
				.withIdentity("epanJob", "group_1").build();
		JobDetail fayuanJob = JobBuilder.newJob(FayuanWatcher.class)
				.withIdentity("fayuanJob", "group_1").build();
		JobDetail odrJob = JobBuilder.newJob(ODRWatcher.class)
				.withIdentity("odrJob", "group_1").build();
		JobDetail aiJob = JobBuilder.newJob(AIWatcher.class)
				.withIdentity("aiJob", "group_1").build();

		Trigger courtTrigger = getTrigger("courtTrigger", "group_1",interval);
		Trigger arbTrigger = getTrigger("arbTrigger", "group_1",interval);
		Trigger epanTrigger = getTrigger("epanTrigger", "group_1",interval);
		Trigger fayuanTrigger = getTrigger("fayuanTrigger", "group_1",interval);
		Trigger aiTrigger = getTrigger("aiTrigger", "group_1",interval);
		Trigger odrTrigger = getTrigger("odrTrigger", "group_1",24);
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched;
		try {
			sched = sf.getScheduler();
			sched.scheduleJob(arbJob, arbTrigger);
			sched.scheduleJob(courtJob, courtTrigger);
			sched.scheduleJob(epanJob, epanTrigger);
			sched.scheduleJob(fayuanJob, fayuanTrigger);
			sched.scheduleJob(odrJob, odrTrigger);
			sched.scheduleJob(aiJob, aiTrigger);
			sched.start();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static Trigger getTrigger(String name, String group,int interval) {
		return TriggerBuilder
				.newTrigger()
				.withIdentity(name, group)
				.startNow()
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.withIntervalInHours(interval) // 时间间隔
								.repeatForever()
				// .withRepeatCount(5) // 重复次数(将执行6次)
				).build();
	}
}
